package com.augurit.wf.bi.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.wf.bi.convert.WfBusTemplateTypeConvertor;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfBusTemplateTypeService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.bi.web.form.WfBusTemplateTypeForm;
import com.augurit.wf.common.dao.WfBusTemplateTypeDao;
import com.augurit.wf.common.entity.WfBusTemplateType;

@Service
@Transactional
public class WfBusTemplateTypeServiceImpl implements IWfBusTemplateTypeService{

	@Autowired
	private WfBusTemplateTypeDao wfBusTemplateTypeDao;
	
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public WfBusTemplateTypeForm get(Long id) {
		WfBusTemplateType entity = wfBusTemplateTypeDao.get(id);
		return WfBusTemplateTypeConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		wfBusTemplateTypeDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(WfBusTemplateTypeForm... forms) {
		if(forms != null)
			for(WfBusTemplateTypeForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(WfBusTemplateTypeForm form){
		
		if(form != null){
			WfBusTemplateType entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = wfBusTemplateTypeDao.get(form.getId());
			}else{
				entity = new WfBusTemplateType();
			}
			
			//属性值转换
			WfBusTemplateTypeConvertor.convertFormToVo(form, entity);
			
			//保存
			wfBusTemplateTypeDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfBusTemplateTypeForm> search(Page<WfBusTemplateTypeForm> page, WfBusTemplateTypeForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from WfBusTemplateType ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		wfBusTemplateTypeDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfBusTemplateTypeForm> list = WfBusTemplateTypeConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<WfBusTemplateTypeForm> search(Page<WfBusTemplateTypeForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<WfBusTemplateType> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		wfBusTemplateTypeDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<WfBusTemplateTypeForm> list = WfBusTemplateTypeConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public List<WfBusTemplateTypeForm> getAll(){
		List<WfBusTemplateType> list = wfBusTemplateTypeDao.getAll();
		return WfBusTemplateTypeConvertor.convertVoListToFormList(list);
	}
	
	@Transactional(readOnly = true)
	public List<BasicCombo> getAllTemplateTypes(){
		List<BasicCombo> result = new ArrayList<BasicCombo>();
		result.add(BasicCombo.getBlankItem());
		
		List<WfBusTemplateType> entities = wfBusTemplateTypeDao.getAll();
		if(entities != null && entities.size() > 0){
			for(WfBusTemplateType entity:entities){
				BasicCombo basicCombo = new BasicCombo();
				basicCombo.setLabel(entity.getTemplateTypeName());
				basicCombo.setValue(entity.getId().toString());
				
				result.add(basicCombo);
			}
		}
		return result;
	}
	
	public void changeTypeName(Long id, String newTypeName){
		if(id != null && newTypeName != null && newTypeName.trim().length() > 0){
			WfBusTemplateType entity = wfBusTemplateTypeDao.get(id);
			entity.setTemplateTypeName(newTypeName);
		}
	}
	
	/**
	 * 获取所有的templateType,并将templateType对应的template也查询出来
	 */
	public List<WfBusTemplateTypeForm> getAllTemplates(){
		List<WfBusTemplateTypeForm> result = getAll();
		if(result != null && result.size() > 0){
			for(WfBusTemplateTypeForm form :result){
				List<WfBusTemplateForm> templateList = wfBusTemplateService.getByTemplateTypeId(form.getId());
				if(templateList != null && templateList.size() > 0)
					form.setTemplateList(templateList);
			}
		}
		return result;
		
	}
}
